package fractal.view;

import fractal.app.AppConfig;
import fractal.app.AppDefine;
import fractal.app.AppInit;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/** Class main application view */
public class AppView extends JFrame {

	private AppInit appInit;
	private AppMenu appMenu;
	private AppPanel appPanel;

	/** constructor arguments: program name, initiation class */
	public AppView(AppInit appInit) {

		//setting:
		super(AppDefine.APPNAME);
		super.setVisible(false);
		super.setLayout(new BorderLayout());

		//references:
		this.appInit = appInit;

		//components:
		this.initSize();
		this.initSkin();
		this.initView();
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(3);
	}

	/** method initiation size application */
	private void initSize() {
		int width = AppConfig.getInstance().getInt(AppDefine.APPWIDTH);
		int height = AppConfig.getInstance().getInt(AppDefine.APPHEIGHT);
		super.setSize(width, height);
	}

	/** method initiation skin application */
	private void initSkin() {
		try {
			if (AppConfig.getInstance().get(AppDefine.APPSKIN).equals(AppDefine.SKINSYS)) {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
				SwingUtilities.updateComponentTreeUI(this);
			} else {
				UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
				SwingUtilities.updateComponentTreeUI(this);
			}
		} catch (Exception ex) {
			this.messageBox("Skin action error!\n" + ex.toString(), 0);
		}
	}

	/** method initiation view application */
	private void initView() {
		this.appMenu = new AppMenu(this);
		this.appPanel = new AppPanel();
		this.appPanel.add(new StartPagePanel(this), AppDefine.STARTNEW);
		this.appPanel.add(new Fractal1Panel(), AppDefine.FRACTAL1);
		this.appPanel.add(new Fractal2Panel(), AppDefine.FRACTAL2);

		super.add(this.appMenu, BorderLayout.NORTH);
		super.add(this.appPanel, BorderLayout.CENTER);
	}

	/** method set skin application */
	public void setSkin(String skinName) {
		this.appInit.saveConfig(AppDefine.APPSKIN, skinName);
		this.appMenu.setSkin(skinName);
		this.initSkin();
	}

	/** method set window application */
	public void setWindow(String panelName) {
		this.appMenu.setWindow(panelName);
		this.appPanel.show(panelName);
		this.repaint();
	}

	/** method set default application */
	public void setDefoult() {
		this.setSkin(AppDefine.SKINDEF);
	}

	/** method view message box */
	public void messageBox(String text, int type) {
		try {
			Toolkit.getDefaultToolkit().beep();
			JOptionPane.showMessageDialog(this, text, AppDefine.APPNAME, type);

		} catch (Exception ex) {
			System.err.println("Message Dialog error!");
			System.err.println("Error message: " + ex.toString());
		}
	}
}